/*
  学习目标：编程式导航
*/

import React from 'react';
import {
  BrowserRouter as Router,
  Route,
  NavLink,
  Switch,
  Redirect,
} from 'react-router-dom';
import './index.css';
import { Link } from 'react-router-dom';

export default class App extends React.Component {
  render() {
    return (
      <Router>
        <div>
          <h1>app组件</h1>
          <NavLink to="/home" exact>
            调到home
          </NavLink>
          <br />
          <NavLink to="/my">调到我的音乐</NavLink>
          <br />
          <NavLink to="/friend">调到朋友</NavLink>
          <Switch>
            <Redirect from="/" to="/home" exact />
            <Route path="/home" component={Home}></Route>
            <Route path="/my" component={MyMusic}></Route>
            <Route path="/friend" component={Friend}></Route>
            <Route component={NotFount}></Route>
          </Switch>
          {/* 💥只有Route组件设置过匹配规则的组件，才有history */}
          {/* 没有经过Route设置匹配规则的组件， 没有history */}
          <Header />
        </div>
      </Router>
    );
  }
}

function Home(props) {
  console.log('  ----->  ', props);
  return (
    <>
      <h1>我是首页组件</h1>
      <button
        onClick={() => {
          // 💥 通过props.history获取操作路由跳转的方式
          // props.history.push('/my');
          props.history.go(-2);
          // goBack等同于go(-1)， 后退一步
          props.history.goBack();
        }}
      >
        点我跳转
      </button>
    </>
  );
}

function Header() {
  return <h1>Header</h1>;
}

function Friend() {
  return (
    <div>
      <Link to="/friend">F1</Link>
      <br />
      <Link to="/friend/f2">F2</Link>
      <br />
      <Link to="/friend/f3">F3</Link>
      <h1>我是朋友组件</h1>
    </div>
  );
}

function NotFount() {
  return <h1>...404..页面</h1>;
}

function MyMusic() {
  return <h1>我是我的音乐件</h1>;
}
